home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / Developer Essentials Jul 90 / International System⁄HyperCard / KanjiTalk Toolkit / Sample Code / KeyScript / KeyScript.Notes < prev    next >
Encoding:
Text File  |  2002-01-01  |  6.0 KB  |  223 lines  |  [TEXT/MPS ]

  1. This is a collection of all the Key Script routines 
  2. from KeyScript Sample Code.
  3.  
  4. {
  5. The following Key Script routines are for Script Manager friendlyness.
  6. They are only active on non-Roman Script Systems.  
  7.  
  8. The objective is to synchronize the keyboard script (input) with the font script (output).
  9. For the Roman script this is easy - the key script is always Roman.  But for other scripts,
  10. like Kanji, it can be either Roman or Japanese.  The user normally selects the key 
  11. script by clicking the script icon or tying command-space. The problem is that the user
  12. typically mixes Roman and Japanese characters together, and he is responsible for 
  13. switching key scripts himself. 
  14.  
  15. Most applications do no special handling and this leads to garbage when a non-roman
  16. key script is used with a roman font.  Other applications (ie HyperCard) try to prevent 
  17. this by forcing the key script to the font script. This makes it very difficult to 
  18. enter roman text when using a foreign script font - you have to keep resetting the key
  19. script.
  20.  
  21. These routines use a combination of setting the script by context and by the previous
  22. state when there is no context.  The result is that the user seldom has to set the key
  23. script and the application seems intelligent.
  24.  
  25. An assumption is that there is a maximum of two scripts installed into the system 
  26. (including Roman).
  27.  
  28. Use find 'keyscript' to locate changes.
  29.  
  30. These routines were written by Joel Cannon, Sept 14, 1989
  31.                             Apple Japan, Developer Technical Support
  32. }
  33.  
  34. VAR
  35.     scriptsInstalled    : INTEGER;
  36.     gKeyScriptState     : INTEGER;
  37.  
  38. {$S Main}
  39. { gets the state for the non-roman key script.  (roman is always roman)}
  40. FUNCTION GetKeyScriptState : INTEGER;
  41. BEGIN
  42.     GetKeyScriptState := gKeyScriptState;
  43. END;
  44.  
  45.  
  46. {$S Main}
  47. { update the non-roman key script state. }
  48. { the state is determined by context and user selection. }
  49. PROCEDURE SetKeyScriptState(txFont : INTEGER);
  50. BEGIN
  51. if scriptsInstalled > 1 then begin
  52.     if Font2Script(txFont) <> smRoman then
  53.         gKeyScriptState := GetEnvirons(smKeyScript);
  54.     end;
  55. END;
  56.  
  57.  
  58. {$S Main}
  59. { calculates and sets the keyboard input script.}
  60. { priority: 
  61.      CONTEXT - [A] selection(from beginning), [B] paragraph (before/after)
  62.      STATE - [C] fontscript, [D] last state, [E] system script.  }
  63. PROCEDURE SetKeyScript(hTE : TEHandle);
  64. VAR
  65.     determined     : BOOLEAN;
  66.     newScript     : INTEGER;
  67.     i             : INTEGER;
  68.     crFound     : BOOLEAN;    
  69.     
  70.     { disregard punctuation, numbers & symbols (single and double byte), 
  71.       detect CR (paragraph markers) and set newscript to the fontscript}
  72.     PROCEDURE GetNewScript;
  73.     VAR
  74.         cType     : INTEGER;
  75.     BEGIN
  76.     IF CharByte(hTE^^.hText^, i) <> smFirstByte THEN BEGIN
  77.         cType:= CharType(hTE^^.hText^, i);
  78.         CASE BitAnd(cType, smcTypeMask) OF 
  79.             smCharPunct : crFound := (CharsHandle(hTE^^.hText)^^[i] = chr(13));{drop out}
  80.             smCharAscii : BEGIN
  81.                 determined := TRUE;
  82.                 newScript := smRoman;
  83.                 END;
  84.             OTHERWISE BEGIN
  85.                 determined := TRUE;
  86.                 newScript := Font2Script(hTE^^.TxFont);
  87.                 END;
  88.             END; {case}    
  89.         END;
  90.     END;
  91.  
  92. BEGIN
  93. IF scriptsInstalled > 1 THEN BEGIN
  94.     determined := FALSE;
  95.     
  96.     {Search inside selection}
  97.     i := hTE^^.SelStart;
  98.     WHILE (i < hTE^^.selEnd) AND NOT determined DO BEGIN
  99.         GetNewScript;
  100.         i := i + 1;
  101.         END;
  102.     
  103.     {Search to beginning of paragraph}
  104.     i := hTE^^.SelStart-1;
  105.     crFound := FALSE;
  106.     WHILE (i >= 0) AND (NOT determined) AND (NOT crFound) DO BEGIN
  107.         GetNewScript;
  108.         i := i - 1;
  109.         END;
  110.     
  111.     {Search to end of paragraph}
  112.     i := hTE^^.SelEnd;
  113.     {** special case - selecting CR includes next paragraph - no visual indication}
  114.     IF (hTE^^.SelStart < hTE^^.SelEnd) then 
  115.         i := i-1;
  116.     crFound := FALSE;
  117.     WHILE (i < hTE^^.teLength) AND (NOT determined) AND (NOT crFound) DO BEGIN
  118.         GetNewScript;
  119.         i := i + 1;
  120.         END;
  121.     
  122.     { when no text in paragraph, use fontscript then state}
  123.     IF NOT determined THEN BEGIN
  124.         IF Font2Script(hTE^^.txFont) = smRoman THEN
  125.             newScript := smRoman
  126.         ELSE
  127.             newScript := GetKeyScriptState;
  128.         END;
  129.  
  130.     { set the new keyscript and update the state}
  131.     IF GetEnvirons(smKeyScript) <> newScript THEN BEGIN
  132.         KeyScript(newScript);
  133.         SetKeyScriptState(hTE^^.txFont);                
  134.         END;
  135.     END;
  136. END; {SetKeyScript}
  137.  
  138.  
  139.  
  140. {$S Main}
  141. { update the key script state }
  142. FUNCTION KeyScriptFilter(theDialog : DialogPtr;  VAR theEvent : EventRecord; 
  143.                        VAR itemHit : INTEGER) : Boolean;
  144. BEGIN
  145. SetKeyScriptState(DialogPeek(theDialog)^.textH^^.txFont);
  146. {
  147. setkeyscript cannot be called within the filter because 
  148. it needs to be called AFTER the event has been handled. 
  149. }
  150. KeyScriptFilter := FALSE;
  151. END;
  152.  
  153.  
  154. {$S Main}
  155. { Display the dialog box in response to the 'About TubeTest' menu item. }
  156. PROCEDURE ShowAboutMeDialog;
  157. Const
  158.     DlgID = 1000;
  159.     OK = 1;
  160.     Text = 2;
  161. VAR        
  162.     theDialog:    DialogPtr;
  163.     itemHit:    Integer;
  164.     hTE : TEHandle;
  165.     itemtype : Integer;
  166.     box : Rect;
  167. BEGIN
  168. theDialog := GetNewDialog(DlgID, NIL, WindowPtr( - 1));
  169.  
  170. { test for two different fonts }
  171. if button then DialogPeek(theDialog)^.textH^^.txFont := Monaco;
  172. SetKeyScript(DialogPeek(theDialog)^.textH);
  173.  
  174. repeat
  175.     ModalDialog(@KeyScriptFilter, itemHit);
  176.     until itemHit = OK;
  177. DisposDialog(theDialog);
  178. END; { ShowAboutMeDialog }
  179.  
  180.  
  181. {$S Initialize}
  182. PROCEDURE InitKeyScript(VAR scriptsInstalled : INTEGER);
  183. CONST
  184.     UnimplCoreRoutine = $9F;
  185.     ScriptUtil = $BF;
  186. BEGIN
  187. {find out if we can use the Script Manager }
  188. scriptsInstalled := 0;
  189. IF GetTrapAddress(UnimplCoreRoutine) <> GetTrapAddress(ScriptUtil) THEN BEGIN
  190.     scriptsInstalled := GetEnvirons(smEnabled);
  191.     SetKeyScriptState(SystemFont);
  192.     END;
  193. END;
  194.  
  195.  
  196. {Integration into application}
  197.  
  198. Init
  199.     InitKeyScript(scriptsInstalled);
  200.  
  201. DoKey
  202.             TEKey(key, te);
  203.             { check for arrow keys }
  204.             if ord(key) in [28,29,30,31] then SetKeyScript(te);
  205.  
  206. DoContent
  207.             TEClick(mouse, shiftDown, DocumentPeek(window)^.docTE);
  208.             SetKeyScript(DocumentPeek(window)^.docTE);                        { JWC }
  209.  
  210. DoActivate
  211.                 TEActivate(docTE);                            {let TE do its thing}    
  212.                 SetKeyScript(docTE);                        { JWC }
  213.  
  214. DoMenu
  215.             SetFont(---);
  216.             SetKeyScript(DocumentPeek(window)^.docTE);                        { JWC }
  217.  
  218.  
  219. DoIdle
  220.         TEIdle(DocumentPeek(window)^.docTE);
  221.         { user key script change events get trapped by system so check here}
  222.         SetKeyScriptState(DocumentPeek(window)^.docTE^^.txFont);
  223.